#!/bin/bash




####### CONSTANTES #############
DEV0=eth0
DEV1='eth0:1'
DEV2='eth0:2'
DEV3='eth0:3'
PPP0='ppp0'
ASTROBOY=192.168.15.0
CHESTER=10.55.7.0
DANIELAN_EL_TERRIBLE=10.31.101.240
FLASH=10.6.3.0
GORDON=10.31.101.160
KRUSTY=10.55.6.0
MAGNETO=10.31.101.244
NEMO=10.4.203.0
ORCO=10.31.101.128
BARRA24=255.255.255.0
BARRA25=255.255.255.128
BARRA26==255.255.255.192
BARRA27=255.255.255.224
BARRA28=255.255.255.240
BARRA29=255.255.255.248
BARRA30=255.255.255.252
H4K=10.55.6.3
H4M=10.31.101.245
H7A=192.168.15.3
H7K=10.55.6.5
H15M=10.31.101.246
H15N=10.4.203.1
H19C=10.55.7.3
H19D=10.31.101.241
H19N=10.4.203.4
H19O=10.31.101.131
H30F=10.6.3.2
H30G=10.31.101.161
H36D=10.31.101.242
H36G=10.31.101.163
HOSTA=10.55.6.6
HOSTB=10.55.7.5
HOSTC=10.31.101.165
WEBSERVER=192.168.15.1
TELSERVERN=10.4.203.129
TELSERVERO=10.31.101.130
FTPSERVER=10.6.3.1
####################


####### COMPUTADORA 1 HOSTA #######
function hosta(){
ifconfig $DEV0 $HOSTA netmask $BARRA24
route add -net $ASTROBOY netmask $BARRA24 gw $H7K
route add -net $CHESTER netmask $BARRA25 gw $H4K
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 gw $H4K
route add -net $FLASH netmask $BARRA24 gw $H4K
route add -net $GORDON netmask $BARRA27 gw $H4K
route add -net $MAGNETO netmask $BARRA30 gw $H4K
route add -net $NEMO netmask $BARRA24 gw $H4K
route add -net $ORCO netmask $BARRA27 gw $H4K
}
####### COMPUTADORA 1 HOSTB #######
function hostb(){
ifconfig $DEV0 $HOSTB netmask $BARRA25
route add -net default gw $H19C
}
####### COMPUTADORA 1 HOSTC #######
function hostc(){
ifconfig $DEV0 $HOSTC netmask $BARRA27
route add -net $ASTROBOY netmask $BARRA24 gw $H36G
route add -net $CHESTER netmask $BARRA25 gw $H36G
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 gw $H36G
route add -net $FLASH netmask $BARRA24 gw $H30G
route add -net $KRUSTY netmask $BARRA24 gw $H36G
route add -net $MAGNETO netmask $BARRA30 gw $H36G
route add -net $NEMO netmask $BARRA24 gw $H36G
route add -net $ORCO netmask $BARRA27 gw $H36G
}
####### COMPUTADORA 2 webserver #######
function webserver(){
ifconfig $DEV0 $WEBSERVER netmask $BARRA24

route add -net default gw $H7A
}
####### COMPUTADORA 2 telserver #######
function telserver(){
ifconfig $DEV0 $TELSERVERN netmask $BARRA24
ifconfig $DEV1 $TELSERVERO netmask $BARRA27
route add -net $ASTROBOY netmask $BARRA24 gw $H15N
route add -net $CHESTER netmask $BARRA25 gw $H19N
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 gw $H19N
route add -net $FLASH netmask $BARRA24 gw $H19N
route add -net $GORDON netmask $BARRA27 gw $H19N
route add -net $KRUSTY netmask $BARRA24 gw $H15N
route add -net $MAGNETO netmask $BARRA30 gw $H15N
route add -net $ORCO netmask $BARRA27 gw $TELSERVERO
route add -net $ORCO netmask $BARRA27 gw $H19N metric 2 
}
####### COMPUTADORA 2 ftpserver #######
function ftpserver(){
ifconfig $DEV0 $FTPSERVER netmask $BARRA24
route add -net default gw $H30F
}
####### COMPUTADORA 3 #######
function router4(){
ifconfig $DEV0 $H4K netmask $BARRA24
ifconfig $PPP0 $H4M
route add -net $ASTROBOY netmask $BARRA24 gw $H7K
route add -net $CHESTER netmask $BARRA25 gw $H15M
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 gw $H15M
route add -net $FLASH netmask $BARRA24 gw $H15M
route add -net $GORDON netmask $BARRA27 gw $H15M
route add -net $KRUSTY netmask $BARRA24 dev $DEV0
route add -net $MAGNETO netmask $BARRA30 dev $DEV1
route add -net $NEMO netmask $BARRA24 gw $H15M
route add -net $ORCO netmask $BARRA27 gw $H15M
}
####### COMPUTADORA 4 #######
function router7(){
ifconfig $DEV0 $H7A netmask $BARRA24
ifconfig $DEV1 $H7K netmask $BARRA24
route add -net $ASTROBOY netmask $BARRA24 dev $DEV0
route add -net $CHESTER netmask $BARRA25 gw $H4K
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 gw $H4K
route add -net $FLASH netmask $BARRA24 gw $H4K
route add -net $GORDON netmask $BARRA27 gw $H4K
route add -net $KRUSTY netmask $BARRA24 dev $DEV1
route add -net $MAGNETO netmask $BARRA30 gw $H4K
route add -net $NEMO netmask $BARRA24 gw $H4K
route add -net $ORCO netmask $BARRA27 gw $H4K
}
####### COMPUTADORA 5 #######
function router15(){
ifconfig $DEV0 $H15N netmask $BARRA24
ifconfig $PPP0 $H15M 
route add -net $ASTROBOY netmask $BARRA24 gw $H4M
route add -net $CHESTER netmask $BARRA25 gw $H19N
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 gw $H19N
route add -net $FLASH netmask $BARRA24 gw $H19N
route add -net $GORDON netmask $BARRA27 gw $H19N
route add -net $KRUSTY netmask $BARRA24 gw $H4M
route add -net $MAGNETO netmask $BARRA30 dev $DEV1
route add -net $NEMO netmask $BARRA24 dev $DEV0
route add -net $ORCO netmask $BARRA27 gw $H19N
}
####### COMPUTADORA 6 #######
function router19(){
ifconfig $DEV0 $H19C netmask $BARRA25
ifconfig $DEV1 $H19D netmask $BARRA30
ifconfig $DEV2 $H19N netmask $BARRA24
ifconfig $DEV3 $H19O netmask $BARRA27

route add -net $ASTROBOY netmask $BARRA24 gw $H15N
route add -net $CHESTER netmask $BARRA25 dev $DEV0
route add -net $DANIELAN_EL_TERRIBLE ntemask $BARRA30 dev $DEV1
route add -net $FLASH netmask $BARRA24 gw $H19D
route add -net $GORDON netmask $BARRA27 gw $H19D
route add -net $KRUSTY netmask $BARRA24 gw $H15N
route add -net $MAGNETO netmask $BARRA30 gw $H15N
route add -net $NEMO netmask $BARRA24 dev $DEV2
route add -net $ORCO netmask $BARRA27 dev $DEV3
}
####### COMPUTADORA 7 #######

function router30(){
ifconfig $DEV0 $H30F netmask $BARRA24
ifconfig $DEV1 $H30G netmask $BARRA27
route add -net $ASTROBOY netmask $BARRA24 gw $H36G
route add -net $CHESTER netmask $BARRA25 gw $H36G
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 gw $H36G
route add -net $FLASH netmask $BARRA24 dev $DEV0
route add -net $GORDON netmask $BARRA27 dev $DEV1
route add -net $KRUSTY netmask $BARRA24 gw $H36G
route add -net $MAGNETO netmask $BARRA30 gw $H36G
route add -net $NEMO netmask $BARRA24 gw $H36G
route add -net $ORCO netmask $BARRA27 gw $H36G
}

####### COMPUTADORA 8 #######

function router36(){
ifconfig $DEV0 $H36D netmask $BARRA30
ifconfig $DEV1 $H36G netmask $BARRA27
route add -net $ASTROBOY netmask $BARRA24 gw $H19D
route add -net $CHESTER netmask $BARRA25 gw $H19D
route add -net $DANIELAN_EL_TERRIBLE netmask $BARRA30 dev $DEV0
route add -net $FLASH netmask $BARRA24 gw $H30G
route add -net $GORDON netmask $BARRA27 dev $DEV1
route add -net $KRUSTY netmask $BARRA24 gw $H19D
route add -net $MAGNETO netmask $BARRA30 gw $H19D
route add -net $NEMO netmask $BARRA24 gw $H19D
route add -net $ORCO netmask $BARRA27 gw $H19D
}

function host (){

	#no me permite enviar peticiones
	/etc/rc.d/rc.ip_forward stop

	echo ""
	echo ""
	echo "Seleccione Host:"
	echo "----------------"
	echo ""
	echo "1) Host a"
	echo "2) Host b"
	echo "3) Host c"

	read opcionHost	
	
	case $opcionHost in
		1) hosta;;
		2) hostb;;
		3) hostc;;
	esac


}

function servidor () {
	
	#no me permite enviar peticiones
	/etc/rc.d/rc.ip_forward stop

	echo ""
	echo "Seleccione Servidor:"
	echo "--------------------"
	echo ""
	echo "1) TelServer"
	echo "2) WebServer"
	echo "3) FTPServer"

	read opcionServidor	

	case $opcionServidor in
     		1) telserver;;
     		2) webserver;;
     		3) ftpserver;;

	esac

}

opcionA=0
echo ""
####borro la tabla de ruteo vieja, y grabo las cosas comunes a todos 
ip route flush all
ip addr flush eth0

#localhost
route add -net 127.0.0.0 netmask 255.0.0.0 gw 127.0.0.0

while [ $opcionA -ne 9 ]
do
	echo ""
	echo "Configurador de Redes:"
	echo "----------------------"
	
	echo "Seleccione opcion de 1 a 8: "
	echo "1) configurar hosts a,b o c "
	echo "2) configurar telserver, webserver o ftpserver"
	echo "3) configurar router 4"
	echo "4) configurar router 7"
	echo "5) configurar router 15"
	echo "6) configurar router 19"
	echo "7) configurar router 30"
	echo "8) configurar router 36"
	echo "9) Salir "
	
	echo "seleccione opcion? "
	read opcionA
	
	case $opcionA in
	1) 
		host;;
	2) 
		servidor;;
	3)
		#me permite enviar peticiones
		/etc/rc.d/rc.ip_forward start
		#cp ./optionsH4 /etc/ppp/options
		pppd ttyS0 noauth noproxyarp passive silent local &
		sleep 3
		router4;;
	4)
		/etc/rc.d/rc.ip_forward start
		router7;;
	5)
		/etc/rc.d/rc.ip_forward start
		#cp ./optionsH15 /etc/ppp/options
		pppd ttyS0 noauth noproxyarp local  "10.31.101.246":"10.31.101.245" &
		sleep 3
		router15;;
	6)
		/etc/rc.d/rc.ip_forward start
		#cp ./optionsH19 /etc/ppp/options
		#pppd ttyS0 noauth noproxyarp passive silent modem &
		sleep 3
		router19;;
	7)
		/etc/rc.d/rc.ip_forward start
		router30;;
	8)
		/etc/rc.d/rc.ip_forward start
		#cp ./optionsH36 /etc/ppp/options
		#pppd ttyS0 noauth noproxyarp modem "10.31.101.242":"10.31.101.241" &
		sleep 3
		router36;;
	9)
		echo "";echo "Programa Terminado.";;
	esac

done

